package org.siteanalyser.service.impl;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.siteanalyser.domain.Message;
import org.siteanalyser.service.Service;
import org.siteanalyser.service.SystemException;
import org.springframework.beans.factory.annotation.Required;

/**
 * Checks if the domain has an entry in the DMOZ directory
 * (http://www.dmoz.org/search/) .
 * 
 * The new feature needs to detect if a domain is listed on DMOZ. For example, the following URL shows that the amazon 
 * domain is listed in the DMOZ directory: http://www.dmoz.org/search/search?q=www.amazon.com. However, the foobar 
 * domain is not: http://www.dmoz.org/search/search?q=www.foobar.com.
 *
 * Test:
 * The associated test class is DmozServiceTest.java. Please add unit tests to this class or other classes as necessary.
 *
 * Implementation:
 * - The domain to check should be retrieved from the message, similar to the FileCheckServiceImpl class. 
 * - The class should add a boolean result to the message report, similar to the FileCheckServiceImpl class. 
 * - If the input domain is listed, 'true' should be added under the 'dmoz' message key. 
 * - If the input domain is not listed, 'false' should be added to the 'dmoz' key. 
 * - Implementation could be similar to the CrawlerServiceImpl class already in the project, 
 * 	 i.e. using the JSoup library to check if the URL exists, then performing some basic processing 
 * 	 to determine if the domain is listed. There are other 3rd party libraries in the project as well, 
 * 	 if you think they may be more suitable, e.g. commons httpclient.
 * - Any reusable utilities could be extracted into HtmlParseUtil or another location, if this will simplify testing
 * 
 */
public class DmozServiceImpl implements Service {
	
	private String dmozURL = "http://www.dmoz.org/search/search?q=";

    public void process(final Message message) throws SystemException {
        makeRequest(message);
    }

    private void makeRequest(final Message message) {
        final String siteNameToCheck = message.getRequest().getUrl();
        Document doc = null;
        try {
            doc = Jsoup.connect(dmozURL+siteNameToCheck).get();
        } catch (final IOException e) {
        	// dmoz site not accessible
            throw new SystemException(e.getMessage());
        }
        if (doc == null) {
            throw new SystemException(String.format("Unable to parse URL: %s", dmozURL+siteNameToCheck));
        }
        // at this moment we have dmoz response
        if(doc.getElementsContainingText("Open Directory Categories").isEmpty() && 
        		doc.getElementsContainingText("Open Directory Sites").isEmpty()) {
        	// site NOT listed
            message.getReport().addReportItem("dmoz", false);
        } else {
        	// site listed
            message.getReport().addReportItem("dmoz", true);
        }
    }

	public String getDmozURL() {
		return dmozURL;
	}

    @Required
	public void setDmozURL(String dmozURL) {
		this.dmozURL = dmozURL;
	}    
}
